package com.cloud.bc.controller.pc;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.config.Config;
import com.cloud.bc.controller.BaseController;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.request.MarketingRequest;
import com.cloud.bc.model.response.TeamCountResponse;
import com.cloud.bc.service.BcSinglesService;
import com.cloud.bc.service.BcUsersService;
import com.cloud.bc.util.JiaoYouPageHelper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

//PC推荐好友
@Controller
@AllArgsConstructor
@RequestMapping("center/marketing")
public class PcCenterMarketingController extends BaseController {
    private final BcUsersService bcUsersService;
    private final BcSinglesService bcSinglesService;
    private final Config config;

    /**
     * 推荐好友
     */
    @GetMapping("")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request, "推荐好友-用户中心", "pc/center/marketing/index");
        view.addObject("bodyCss", " page_manage page_marketing_index");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        String eidEncode = bcSinglesService.getEidEncode(userInfo.getId().toString());
        String url = config.getWebSite() + "recruit?eid=" + eidEncode;
        view.addObject("marketingUrl", url);
        return view;
    }

    /**
     * 我推荐的人
     */
    @GetMapping("team")
    public ModelAndView team(HttpServletRequest request, HttpServletResponse response,
                             MarketingRequest req) {
        ModelAndView view = baseView(request, "我推荐的人-用户中心", "pc/center/marketing/team");
        view.addObject("bodyCss", "page_manage page_marketing_team");
        Page page = new Page();
        page.setCurrent(Convert.toInt(req.getPage(), 1));
        BcSingles bcSingles = new BcSingles();

        bcSingles.setGender(req.getGender());
        IPage<BcSingles> bcSinglesIPage = bcSinglesService.selectPage(page, bcSingles);

        List<BcSingles> records = bcSinglesIPage.getRecords();
        records.forEach(item -> {
            item.setAge(bcSinglesService.ageConvert(item.getBirthday()));
        });
        view.addObject("teamList", records);
        JiaoYouPageHelper jiaoYouPageHelper = new JiaoYouPageHelper();
        view.addObject("page", jiaoYouPageHelper.getBcPageHTML(page, request));
        return view;
    }

}
